import os

def getdeviceslist() -> list:
    all_devices = []
    cmd = "tidevice list"
    reslut = os.popen(cmd).readlines()
    print(reslut)
    for item in reslut[:-1]:
        if item != "\n":
            all_devices.append(str(item).split("\t")[0].split(' ')[0])
    return all_devices


def getplatfoemversion(devices: str) -> str:
    cmd = 'tidevice --udid %s info' % devices
    reslut = os.popen(cmd).readlines()
    for i in reslut:
        if 'ProductVersion' in i:
            return ((i).split(" ")[-1].split('\\n')[0])
    return ''


def iosisinstall(packname: str, devices: str) -> bool:
    cmd = "tidevice  --udid {} applist".format(devices)
    reslut = os.popen(cmd).readlines()
    all_apkname = []
    for i in reslut:
        apkname = str(i).split(' ')[0]
        all_apkname.append(apkname)
    if packname in all_apkname:
        return True
    return False


def uninstallapk(packname: str, devices: str) -> bool:
    reslut = iosisinstall(packname, devices)
    if reslut:
        cmd = 'tidevice  --udid %s uninstall %s ' % (devices, packname)
        os.system(cmd)
        return True
    return False


def installios(paknamepath: str, devices: str) -> bool:
    cmd = 'tidevice --udid  %s install %s' % (devices, paknamepath)
    os.system(cmd)
    return True



if __name__ == '__main__':
    print(getdeviceslist())
    print(getplatfoemversion('00008020-001539623A05002E'))